<template>
  <div>
    <FormPanel ref="from" :list="formList" @formChange="changeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <div style="height: 47px"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <multiuse-button type="primary" :click="dataHandle.bind(this, 60761002)">提交</multiuse-button>
      <multiuse-button type="primary" :click="dataHandle.bind(this, 60761001)">保存</multiuse-button>
      <el-button @click="closeDrawer">返回</el-button>
    </div>

    <BaseDialog :visible.sync="isShowVin" :title="'新增'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <SearchVin :rowData="formData" @close="closeDialog" />
    </BaseDialog>
  </div>
</template>
<script>
import moment from 'moment';
import { dictionary } from '@/utils/dictMixin';
import { saveData } from '@/api/dealerSale/TestDrive/apply';
import SearchVin from './SearchVin';

export default {
  name: 'addModel',
  mixins: [dictionary],
  props: ['rowData'], // 上个页面带入的值
  data() {
    this.saveData = null;
    return {
      formList: this.createdFrom(),
      formData: {},
      isShowVin: false,
      vehicleId: ''
    };
  },
  components: {
    SearchVin
  },
  created() {},
  mounted() {},
  methods: {
    // 查询VIN
    vinHandle() {
      this.isShowVin = true;
      this.formData.vehicleStatus = this.formList.find(x => x.fieldName == 'orderType').initialValue;
    },
    closeDialog(val, data) {
      this.isShowVin = val;
      if (data && data.length > 0) {
        this.vehicleId = data[0].vehicleId;
        this.formList.find(x => x.fieldName == 'vin').initialValue = data[0].vin;
      }
      console.log(data, '---关闭VIN---');
    },
    async dataHandle(status) {
      this.$refs.from.SUBMIT_FORM();
      if (this.saveData) {
        this.saveData.vehicleId = this.vehicleId;

        this.saveData.checkStatus = status;
        let res = await saveData(this.saveData);

        if (res.resultCode == 200) {
          this.$notify.success({ title: '提示信息', message: '操作成功' });
          this.$emit('close', false, 'refresh');
        }
      }
    },
    closeDrawer() {
      this.$emit('close');
    },
    changeHandle(val) {
      this.saveData = val;
    },
    createdFrom() {
      return [
        {
          type: 'SELECT',
          label: '订单类型',
          fieldName: 'orderType',
          initialValue: 60401003,
          clearable: false,
          offsetRightCols: 2,
          itemList: [
            { text: '在途车', value: 60401002 },
            { text: '库存车', value: 60401003 }
          ],
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
          change: val => {
            if (val) {
              this.formList.find(x => x.fieldName == 'vin').initialValue = '';
              this.formList.find(x => x.fieldName == 'isFirst').initialValue = '';
              return;
            }
            this.formList.find(x => x.fieldName == 'vin').initialValue = '';
            this.formList.find(x => x.fieldName == 'isFirst').initialValue = '';
          }
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '',
          readonly: true,
          rules: [{ required: true, message: '请搜索', trigger: 'change' }],
          unitRender: () => {
            return <el-button slot="append" icon="el-icon-search" onClick={() => this.vinHandle()}></el-button>;
          }
        },
        // {
        //   type: 'SELECT',
        //   label: '是否首台',
        //   fieldName: 'isFirst',
        //   placeholder: '请选择',
        //   itemList: this.createDictList('1004'),
        //   rules: [{ required: true, message: '请选择', trigger: 'change' }]
        // },
        {
          type: 'INPUT',
          label: '提报数量',
          fieldName: 'reportCount',
          placeholder: '',
          initialValue: 1,
          disabled: true
        },
        {
          type: 'TEXT_AREA',
          label: '备注',
          fieldName: 'remark',
          selfCols: 3,
          rows: 2
        }
      ];
    }
  }
};
</script>
